package com.dididoctor.patient.Activity.Usercentre.Userinfo;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

/**
 * Created by Administrator on 2016/4/8.
 */
public class UserInfoPresenter extends BasePresenter {

    private UserInfoView view;

    public UserInfoPresenter(Context context, UserInfoView view) {
        super(context, view);
        this.view = view;
    }

    public void updateuserinfo(String phone, String name, String headPic, String sex, String birth, String idNo, String cityId, String address) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("phone", phone);
        params.put("name", name);
        params.put("token", Token.getToken());
        params.put("headPic", headPic);
        params.put("sex", sex);
        params.put("birth", birth);
        params.put("idNo", idNo);
        params.put("cityCode", cityId);
        params.put("address", address);
//        Log.d("aaaa", ConstantValue.userupdate + "?userId = " + Token.getId() + "&phone=" + phone + "&name=" + name +
//                "&token= " + Token.getToken() + "&headpic=" + headPic + "&sex=" + sex + "&birth=" + birth + "&idNo=" + idNo + "&cityId=" + cityId + "&address=" + address);
        BusinessClient.post(ConstantValue.userupdate, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.savesucced();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.showToastMessage(throwable.getMessage());
                view.savefail();
            }
        });
    }

    public void getuserinfo() {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.userinfo, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Userinfo userinfo = new Userinfo();
                    userinfo.setId(response.getString("userId"));
                    userinfo.setPhone(response.getString("phone"));
                    userinfo.setName(response.getString("name"));
                    userinfo.setHeadPic(response.getString("headPic"));
                    userinfo.setSex(response.getString("sex"));
                    userinfo.setBirth(response.getString("birth"));
                    userinfo.setIdNo(response.getString("idNo"));
                    userinfo.setCity(response.getString("city"));
                    userinfo.setAddress(response.getString("address"));
                    userinfo.setAccId(response.getString("accId"));
                    userinfo.setAccPwd(response.getString("accPwd"));
                    userinfo.setToken(response.getString("token"));
                    userinfo.setCityName(response.getString("cityName"));
                    view.getUserInfo(userinfo);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getUserInfofail();
            }
        });
    }
}
